package itsm.isperp.module.controller.change;

import java.util.List;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.change.ChgRiskElement;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.change.ChgRiskElementService;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 服务控制器
 * 
 * @author zhg
 * 
 */
@Controller
@RequestMapping("change/riskElement")
public class ChgChangeRiskElementController extends BaseController<ChgRiskElement> {

	/**
	 * 
	 */
	@Autowired
	protected ChgRiskElementService chgRiskElementService;

	@Autowired
	protected AppDictService appDictService;

	@Override
	public ChgRiskElementService getService() {
		return chgRiskElementService;
	}

	@Override
	public String getPageTitle() {
		return "影响因素";
	}

	/**
	 * 用户编辑界面
	 * 
	 * @return 用户编辑界面视图
	 */
	@RequestMapping("edit")
	@Override
	public ModelAndView edit(String id, HttpServletRequest req) {
		ModelAndView mv = this.getModelAndView();

		ChgRiskElementService chgRiskElementService = ContextHolder
				.getSpringBean("chgRiskElementService");

		ChgRiskElement model = this.getService().findOne(id);
		if (model == null) {
			model = new ChgRiskElement();
			model.setId(HibernateUUIDGenerator.generate());
		} else {
			ChgRiskElement chgRiskElement = chgRiskElementService.findOne(model
					.getCategoryId());
			if (chgRiskElement != null) {
				mv.addObject("categoryName", chgRiskElement.getName());
			}

		}

		mv.addObject("model", model);

		return mv;
	}


	@Override
	@RequestMapping(value = { "manage", "" })
	public ModelAndView manage(HttpServletRequest req) {
		ModelAndView mv = super.manage(req);
		mv.addObject("category",
				appDictService.findByType("SERVICE_STATUS"));

		return mv;
	}
	
	@RequestMapping("selectRiskEdit")
	public ModelAndView selectRiskEdit(HttpServletRequest req) {
		ModelAndView mv = new ModelAndView("change/riskElement/selectEdit");
		JqGridRequest request=new JqGridRequest();
		mv.addObject("risk_category",
				appDictService.findByType("CHANGE_RISK_CATEGORY"));
		request.setSort("order_num", Direction.DESC);
		PageResponse<ChgRiskElement> list=this.getService().findAll(request).toPage();
		mv.addObject("risk_elementlist",list);
		return mv; 
	}
	
	@RequestMapping("getChgRiskElementList")
	@ResponseBody
	public List<ChgRiskElement> getChgRiskElementList(JqGridRequest request, HttpServletRequest req) {
		String sourceId = req.getParameter("sourceId");
		ChgRiskElementService chgRiskElementService = ContextHolder.getSpringBean("chgRiskElementService");
		request.setSearchString(" id in(select to_id from chg_element_relation where from_id='" + sourceId + "' and to_type='chgRiskElement')");
		List<ChgRiskElement> list = chgRiskElementService.findAll(request);
		return list;
	}
}

